import 'package:flutter/material.dart';

import '../../config/public_color.dart';

/// FileName linear_gradient_decoration
///
/// @Author lax
/// @Date 2023/3/30
///
/// @Description

class LinearGradientDecoration extends BoxDecoration {
  static Alignment defaultBegin = Alignment.centerLeft;

  static Alignment defaultEnd = Alignment.centerRight;

  static List<Color>? defaultColors;

  LinearGradientDecoration({
    double? radius,
    BorderRadius? borderRadius,
    Alignment? begin,
    Alignment? end,
    List<Color>? colors,
    List<BoxShadow>? boxShadow,
  }) : super(
          borderRadius: borderRadius ?? BorderRadius.circular(radius ?? 0),
          boxShadow: boxShadow,
          gradient: LinearGradient(
            colors: getColors(colors),
            begin: begin ?? defaultBegin,
            end: end ?? defaultEnd,
          ),
        );

  static LinearGradientDecoration horizontal({
    double? radius,
    BorderRadius? borderRadius,
    List<Color>? colors,
    List<BoxShadow>? boxShadow,
  }) {
    return LinearGradientDecoration(
      radius: radius,
      borderRadius: borderRadius ?? BorderRadius.circular(radius ?? 0),
      begin: Alignment.centerLeft,
      end: Alignment.centerRight,
      colors: colors,
      boxShadow: boxShadow,
    );
  }

  static LinearGradientDecoration vertical({
    double? radius,
    BorderRadius? borderRadius,
    List<Color>? colors,
    List<BoxShadow>? boxShadow,
  }) {
    return LinearGradientDecoration(
      radius: radius,
      borderRadius: borderRadius ?? BorderRadius.circular(radius ?? 0),
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: colors,
      boxShadow: boxShadow,
    );
  }

  static Gradient getLinearGradient({
    Color? color,
    List<Color>? colors,
    Alignment? begin,
    Alignment? end,
  }) {
    return LinearGradient(
      colors: getColors(colors ?? [if (color != null) color]),
      begin: begin ?? defaultBegin,
      end: end ?? defaultEnd,
    );
  }

  static List<Color> getColors(List<Color>? colors) {
    colors ??= defaultColors ??
        [
          PublicColor.lightPrimary,
          PublicColor.primary,
        ];
    if (colors.isEmpty) {
      colors = [Colors.transparent, Colors.transparent];
    }
    if (colors.length == 1) {
      colors = [colors.first, colors.first];
    }
    return colors;
  }
}
